﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace YiTian.Core.Modularity
{
    /// <summary>
    /// 模块基础类
    /// </summary>
    public abstract class ModuleBase:IModule
    {
        /// <summary>
        /// 通过DependsOn特性获取当前模块的依赖
        /// </summary>
        /// <returns>依赖的模块类型集合</returns>
        public virtual Type[] GetDependencies()
        {
            // 反射获取当前类型上的DependsOn特性
            var dependsOnAttributes = GetType().GetCustomAttributes<DependsOnAttribute>(inherit: true);
            // 合并所有特性中的依赖模块类型
            return dependsOnAttributes.SelectMany(attr => attr.DependentModuleTypes).ToArray();
        }

        /// <summary>
        /// 服务配置默认实现（空方法，子类可重写）
        /// </summary>
        /// <param name="context"></param>
        public virtual void ConfigureServices(ModuleConfigurationContext context) { }
        /// <summary>
        /// 模块初始化(应用启动时执行)
        /// </summary>
        public virtual void Initialize(ModuleInitializationContext context) { }

        /// <summary>
        /// 模块销毁(应用关闭时执行)
        /// </summary>
        public virtual void Shutdown() { }

    }
}
